package com.songcha.library_business.proxy.database

import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.TimeUtil
import com.songcha.library_database_douyue.bean.BookTodayReadChapterCountDBBean
import com.songcha.library_database_douyue.bean.BookTodayReadTimeDBBean
import com.songcha.library_database_douyue.greendao.BookTodayReadChapterCountDBBeanDao
import com.songcha.library_database_douyue.greendao.BookTodayReadTimeDBBeanDao
import com.songcha.library_database_douyue.manager.GreenDaoManager


class BookTodayReadChapterCountDaoManager {
    companion object{
        //return value use bit compute,if first bit is 1,represent new value,0 represent already save
        @JvmStatic
        fun addTodayReadChapterCount(startChapter:Int,endChapter:Int):Int{
            val chapterCount=(endChapter-startChapter)
            if(chapterCount<=0) return 0
            try{
                val dao=GreenDaoManager.getInstance().bookTodayReadChapterCountDBBeanDao
                val bean=BookTodayReadChapterCountDBBean()
                bean.userId=UserInfoManager.userId
                val qb=dao.queryBuilder().where(BookTodayReadChapterCountDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
                val list=qb.list()
                if(list.size==0){
                    bean.readChapterCount=chapterCount
                    bean.startReadTime=TimeUtil.getCurrentTimeSecond()
                    dao.insert(bean)
                    return bean.readChapterCount or (1 shl 31)
                }else {
                    val item=list[0]
                    //whether today read
                    val todayZero=TimeUtil.getTodayZeroTimeStamp()
                    bean.id=item.id
                    if(item.startReadTime>=todayZero && item.startReadTime<todayZero+86400){
                        bean.readChapterCount=item.readChapterCount+chapterCount
                        bean.startReadTime=item.startReadTime
                        dao.update(bean)
                        return bean.readChapterCount
                    }else{
                        //pick today read chapter
                        //TODO yesterday read chapter count is unknow
                        bean.readChapterCount=chapterCount
                        bean.startReadTime=TimeUtil.getCurrentTimeSecond()
                        dao.update(bean)
                        return bean.readChapterCount or (1 shl 31)
                    }

                }
            }catch (e:Exception){
                LogUtil.log("保存当天阅读章节数异常")
                e.printStackTrace()
                return 0
            }
        }

        @JvmStatic
        fun getTodayReadChapterCount():Int{
            val dao=GreenDaoManager.getInstance().bookTodayReadChapterCountDBBeanDao
            val qb=dao.queryBuilder().where(BookTodayReadChapterCountDBBeanDao.Properties.UserId.eq(UserInfoManager.userId))
            val list=qb.list()
            if(list.size>0){
                val item=list[0]
                val todayZero=TimeUtil.getTodayZeroTimeStamp()
                if(item.startReadTime>=todayZero && item.startReadTime<todayZero+86400){
                    return item.readChapterCount
                }else{
                    val bean=BookTodayReadChapterCountDBBean()
                    bean.id=item.id
                    bean.userId=UserInfoManager.userId
                    bean.readChapterCount=0
                    bean.startReadTime=TimeUtil.getCurrentTimeSecond()
                    dao.update(bean)
                    return 0
                }
            }else{
                return 0
            }
        }

    }
}